package com.eclink.asset.setup.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eclink.asset.domain.query.WarehouseQuery;
import com.eclink.asset.setup.domain.dao.Company;
import com.eclink.asset.setup.mapper.CompanyMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 公司Service
 *
 * @author chenshixian
 */
@Service
public class CompanyService {

    @Resource
    private CompanyMapper mapper;

    /**
     * 查询
     *
     * @param id ID
     * @return 结果
     */
    public Company selectById(Long id) {
        return mapper.selectById(id);
    }

    /**
     * 查询仓库名称
     *
     * @param id 仓库主键
     * @return 仓库
     */
    public String getNameById(Long id) {
        Company warehouse = selectById(id);
        if (warehouse == null) {
            return null;
        }
        return warehouse.getName();
    }

    /**
     * 查询仓库列表
     *
     * @param page 分页条件
     * @return 仓库
     */
    public Page<Company> selectList(Pageable page) {

        QueryWrapper<Company> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        qw.orderByDesc("id");
        return mapper.selectPage(new Page<>(page.getPageNumber(), page.getPageSize()), qw);
    }

    /**
     * 查询所有
     *
     * @return 仓库
     */
    public List<Company> selectListAll() {

        QueryWrapper<Company> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        return mapper.selectList(qw);
    }

    /**
     * 新增仓库
     *
     * @param warehouse 仓库
     * @return 结果
     */
    public int insert(Company warehouse) {
        warehouse.setDelFlag(0);
        warehouse.setCreateTime(LocalDateTime.now());
        return mapper.insert(warehouse);
    }

    /**
     * 修改仓库
     *
     * @param warehouse 仓库
     * @return 结果
     */
    public int update(Company warehouse) {
        return mapper.updateById(warehouse);
    }


    /**
     * 删除仓库信息
     *
     * @param id 仓库主键
     * @return 结果
     */
    public int deleteById(Long id) {
        return mapper.deleteById(id);
    }

    public void deleteByIds(Long[] ids) {
        for(Long id : ids) {
            deleteById(id);
        }
    }

    /**
     * 根据主键集合查询仓库列表
     *
     * @param ids 主键集合
     * @return 仓库列表
     */
    public List<Company> selectByIdIn(Collection<Long> ids) {
        // 如果主键集合为空，直接返回空集合
        if (ids == null || ids.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<Company> qw = new QueryWrapper<>();
        qw.in("id",ids);
        return mapper.selectList(qw);
    }
}
